<template>
	<el-card>
		<el-form :inline="true" :model="state.queryForm" @keyup.enter="getDataList()">
			<el-form-item>
				<el-button @click="getDataList()">查询</el-button>
			</el-form-item>
			<el-form-item>
				<!-- <el-button v-auth="'site:article:save'" type="primary" @click="addOrUpdateHandle()">新增</el-button> -->
				<el-button v-auth="'site:article:save'" type="primary" @click="write(null)">写文章</el-button>
			</el-form-item>
			<el-form-item>
				<el-button v-auth="'site:article:delete'" type="danger" @click="deleteBatchHandle()">删除</el-button>
			</el-form-item>
			<el-form-item>
				<!-- <el-button v-auth="'site:article:save'" type="primary" @click="write()">写文章</el-button> -->
			</el-form-item>
		</el-form>
		<el-table v-loading="state.dataListLoading" :data="state.dataList" style="width: 100%" @selection-change="selectionChangeHandle">
			<el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
			<el-table-column prop="id" label="id" width="70"></el-table-column>
			<el-table-column prop="title" label="标题" width="500">
				<template #default="scope">
					<span style="font-weight: bold; display: block">{{ scope.row.title }}</span>
				</template>
			</el-table-column>
			<el-table-column prop="content" label="内容" show-overflow-tooltip></el-table-column>
			<el-table-column prop="categoryId" label="分类" header-align="center" align="center"></el-table-column>
			<el-table-column prop="tagIds" label="标签" header-align="center" align="center"></el-table-column>
			<el-table-column prop="up" label="点赞" header-align="center" align="center" width="60"></el-table-column>
			<el-table-column prop="down" label="踩" header-align="center" align="center" width="60"></el-table-column>
			<el-table-column prop="like" label="喜欢" header-align="center" align="center" width="60"></el-table-column>
			<el-table-column prop="collect" label="收藏" header-align="center" align="center" width="60"></el-table-column>
			<el-table-column prop="comment" label="评论" header-align="center" align="center" width="60"></el-table-column>
			<el-table-column prop="cover" label="封面" header-align="center" align="center">
				<template #default="scope">
					<el-image :src="scope.row.cover" fit="scale-down"> </el-image>
				</template>
			</el-table-column>
			<fast-table-column prop="status" label="状态" dict-type="article_status" header-align="center" align="center" width="70"></fast-table-column>
			<!-- <el-table-column prop="version" label="版本号" header-align="center" align="center"></el-table-column> -->
			<!-- <el-table-column prop="deleted" label="删除标识  0：正常   1：已删除" header-align="center" align="center"></el-table-column> -->
			<el-table-column prop="creator" label="创建者" header-align="center" align="center"></el-table-column>
			<el-table-column prop="createTime" label="创建时间" width="155"></el-table-column>
			<el-table-column prop="updater" label="更新者" header-align="center" align="center"></el-table-column>
			<el-table-column prop="updateTime" label="更新时间" width="155"></el-table-column>
			<el-table-column label="操作" fixed="right" header-align="center" align="center" width="150">
				<template #default="scope">
					<!-- <el-button v-auth="'site:article:update'" type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button> -->
					<el-button v-auth="'site:article:update'" type="text" size="small" @click="write(scope.row.id)">修改</el-button>
					<el-button v-auth="'site:article:delete'" type="text" size="small" @click="deleteBatchHandle(scope.row.id)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<el-pagination
			:current-page="state.page"
			:page-sizes="state.pageSizes"
			:page-size="state.limit"
			:total="state.total"
			layout="total, sizes, prev, pager, next, jumper"
			@size-change="sizeChangeHandle"
			@current-change="currentChangeHandle"
		>
		</el-pagination>

		<!-- 弹窗, 新增 / 修改 -->
		<add-or-update ref="addOrUpdateRef" @refreshDataList="getDataList"></add-or-update>
	</el-card>
</template>

<script setup lang="ts" name="siteArticleIndex">
import { useCrud } from '@/hooks'
import { reactive, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'

// import AddOrUpdate from './add-or-update.vue'
import { IHooksOptions } from '@/hooks/interface'

// import MarkdownEditor from '@/components/markdown-editor'
const router = useRouter()

const state: IHooksOptions = reactive({
	dataListUrl: '/site/article/page',
	deleteUrl: '/site/article',
	queryForm: {}
})

const addOrUpdateRef = ref()
const addOrUpdateHandle = (id?: number) => {
	addOrUpdateRef.value.init(id)
}

const { getDataList, selectionChangeHandle, sizeChangeHandle, currentChangeHandle, deleteBatchHandle } = useCrud(state)

// 写文章
const write = (id: any) => {
	router.push({ path: '/site/article/writeArticle', query: { id } })
}
</script>
